
#import "SceneRanking.h"
#import "SceneMenu.h"
#import "global.h"
#import "OpenFeint.h"


@implementation Ranking

+(id) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	Ranking *layer = [Ranking node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if ( (self=[super init] )) 
	{
		m_windowSize = [[CCDirector sharedDirector] winSize];
		
		if ( m_windowSize.width == 768 )
			m_DeviceKind = DEVICE_IPAD;
		else
			m_DeviceKind = DEVICE_IPHONE;
		
		//bg image
		spBackground = [CCSprite spriteWithFile:@"background2.png"];
		if ( m_DeviceKind == DEVICE_IPHONE )
			[spBackground setPosition:ccp(CX,CY-145)];
		else 
			[spBackground setPosition:ccp(CX,CY)];
		[self addChild:spBackground];
		
		CCSprite *spWhiteboard = [CCSprite spriteWithFile:@"whiteboard.png"];
		[spWhiteboard setPosition:ccp(CX,CY+90)];
		[self addChild:spWhiteboard];
		
		//bg sub image
		spBall1 = [CCSprite spriteWithFile: @"ball.png"];
		[spBall1 setScale:0.7f];
		[spBall1 setPosition: ccp(CX-85,CY+15)];
		[self addChild:spBall1];
		spBall2 = [CCSprite spriteWithFile: @"ball.png"];
		[spBall2 setScale:0.7f];
		[spBall2 setPosition: ccp(CX-85,CY-18)];
		[self addChild:spBall2];
		
		id rotate1 = [CCRotateBy actionWithDuration:1 angle:360];
		id repeat1 = [CCRepeatForever actionWithAction:rotate1];
		[spBall1 runAction:repeat1];		
		id rotate2 = [CCRotateBy actionWithDuration:1 angle:360];
		id repeat2 = [CCRepeatForever actionWithAction:rotate2];
		[spBall2 runAction:repeat2];		

		//ranking
		NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
		if ( [prefs boolForKey:@"ranking_0"] )
			gameInfo.nRanking[0] = [prefs integerForKey:@"ranking_0"];
		if ( [prefs boolForKey:@"ranking_1"] )
			gameInfo.nRanking[1] = [prefs integerForKey:@"ranking_1"];
		if ( [prefs boolForKey:@"ranking_2"] )
			gameInfo.nRanking[2] = [prefs integerForKey:@"ranking_2"];
		if ( [prefs boolForKey:@"ranking_3"] )
			gameInfo.nRanking[3] = [prefs integerForKey:@"ranking_3"];
		if ( [prefs boolForKey:@"ranking_4"] )
			gameInfo.nRanking[4] = [prefs integerForKey:@"ranking_4"];
		
		NSString *strRanking0 = [NSString stringWithFormat:@"1st: %d", gameInfo.nRanking[0]];
		labelRanking0 = [CCLabel labelWithString:strRanking0 fontName:@"MarkerFelt-Thin" fontSize:20];
		[self addChild: labelRanking0];
		[labelRanking0 setColor:ccc3(0,0,0)];
		[labelRanking0 setPosition: ccp(CX, CY+200)];
		
		NSString *strRanking1 = [NSString stringWithFormat:@"2nd: %d", gameInfo.nRanking[1]];
		labelRanking1 = [CCLabel labelWithString:strRanking1 fontName:@"MarkerFelt-Thin" fontSize:20];
		[self addChild: labelRanking1];
		[labelRanking1 setColor:ccc3(0,0,0)];
		[labelRanking1 setPosition: ccp(CX, CY+165)];
		
		NSString *strRanking2 = [NSString stringWithFormat:@"3rd: %d", gameInfo.nRanking[2]];
		labelRanking2 = [CCLabel labelWithString:strRanking2 fontName:@"MarkerFelt-Thin" fontSize:20];
		[self addChild: labelRanking2];
		[labelRanking2 setColor:ccc3(0,0,0)];
		[labelRanking2 setPosition: ccp(CX, CY+130)];
		
		NSString *strRanking3 = [NSString stringWithFormat:@"4th: %d", gameInfo.nRanking[3]];
		labelRanking3 = [CCLabel labelWithString:strRanking3 fontName:@"MarkerFelt-Thin" fontSize:20];
		[self addChild: labelRanking3];
		[labelRanking3 setColor:ccc3(0,0,0)];
		[labelRanking3 setPosition: ccp(CX, CY+95)];
		
		NSString *strRanking4 = [NSString stringWithFormat:@"5th: %d", gameInfo.nRanking[4]];
		labelRanking4 = [CCLabel labelWithString:strRanking4 fontName:@"MarkerFelt-Thin" fontSize:20];
		[self addChild: labelRanking4];
		[labelRanking4 setColor:ccc3(0,0,0)];
		[labelRanking4 setPosition: ccp(CX, CY+60)];
						
		//menu
		[CCMenuItemFont setFontSize:25];
        [CCMenuItemFont setFontName:@"MarkerFelt-Thin"];
		
		CCMenuItem *online_ranking = [CCMenuItemFont itemFromString:@"online ranking" target:self selector:@selector(onlineRanking:)];
		[((CCMenuItemFont *)online_ranking).label setColor:ccc3(0,0,0)];	
        CCMenuItem *over = [CCMenuItemFont itemFromString:@"back to menu" target:self selector:@selector(gotoMenu:)];
		[((CCMenuItemFont *)over).label setColor:ccc3(0,0,0)];	
		
		CCMenu *menu = [CCMenu menuWithItems:online_ranking, over, nil];	
        [menu alignItemsVertically];
        [self addChild:menu z:1];
		
		//etc
		isTouchEnabled = YES;

	}
	return self;
}

-(void)onlineRanking: (id)sender
{
	[OpenFeint launchDashboardWithHighscorePage:(NSString*)@"448694"];
}

-(void)gotoMenu: (id)sender {
    Menu *ms = [Menu node];
    [[CCDirector sharedDirector] replaceScene: (CCScene*)ms];
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end





